<template>
  <div class="account-settings-info-view">
    <a-row
      :gutter="16"
      type="flex"
      justify="center">
      <a-col
        :order="isMobile ? 2 : 1"
        :md="24"
        :lg="16">

        <a-form
          :form="form"
          layout="vertical">
          <a-form-item :label="$t('account.settings.basic.nickname')">
            <a-input
              style="width: 300px"
              v-decorator="['name', {rules: [{required: true, min: 1, message: '用户昵称不能为空'}]}]"
              :placeholder="$t('account.settings.basic.nickname-message')" />
          </a-form-item>
          <a-form-item :label="$t('account.settings.basic.profile')">
            <a-textarea
              rows="4"
              v-decorator="['profile', {rules: [{message: '请输入个人介绍'}]}]"
              :placeholder="$t('account.settings.basic.profile-message')" />
          </a-form-item>
          <a-form-item
            :label="$t('account.settings.basic.phone')"
            :required="false">
            <a-input
              style="width: 200px"
              v-decorator="['phone', {rules: [{message: '请输入正确手机号'}]}]"
              :placeholder="$t('account.settings.basic.phone-message')" />
          </a-form-item>
          <a-form-item
            :label="$t('account.settings.basic.country')"
            :required="false">
            <a-select
              style="width: 200px"
              v-decorator="['country',{rules: [{message: '请选择所属国家'}]}]"
              :placeholder="$t('account.settings.basic.country-message')">
              <a-select-option value="China">
                中国
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item
            :label="$t('account.settings.basic.geographic')">
            <a-cascader
              style="width: 400px"
              :options="geographicList"
              :fieldNames="{ label: 'region_name', value: 'region_id', children: 'children' }"
              :showSearch="true"
              expandTrigger="hover"
              changeOnSelect
              v-model="userInfo.geographic"
              @change="geographicChange"
              :placeholder="$t('account.settings.basic.geographic-message')" />
          </a-form-item>
          <a-form-item
            :label="$t('account.settings.basic.address')"
            :required="false">
            <a-input
              style="width: 500px"
              v-decorator="['address',{rules: [{message: '请输入地址'}]}]"
              :placeholder="$t('account.settings.basic.address-message')" />
          </a-form-item>

          <a-form-item>
            <a-button
              type="primary"
              @click="handleSubmit">{{ $t('account.settings.basic.update') }}</a-button>
          </a-form-item>
        </a-form>

      </a-col>
      <a-col
        :order="1"
        :md="24"
        :lg="8"
        :style="{ minHeight: '180px' }">
        <div
          class="ant-upload-preview"
          @click="$refs.modal.edit(userInfo.avatar)">
          <a-icon
            type="cloud-upload-o"
            class="upload-icon" />
          <div class="mask">
            <a-icon type="plus" />
          </div>
          <img :src="userInfo.avatar" />
        </div>
      </a-col>

    </a-row>

    <avatar-modal
      ref="modal"
      @ok="setavatar" />

  </div>
</template>

<script>
import AvatarModal from './AvatarModal'
import { mapGetters } from 'vuex'
import { update } from '@/api/login'
import { getGeographic } from '@/api/system/sysgeographic'
import { baseMixin } from '@/store/app-mixin'
import { merge, pick } from 'lodash'
const filterGeographic = (list) => {
  if (list === null) return []
  const re = list.map(
    (item) => {
      const result = {
        region_id: item.region_id,
        region_name: item.region_name
      }
      return result
    }
  )
  return re
}
export default {
  mixins: [baseMixin],
  components: {
    AvatarModal
  },
  data () {
    return {
      form: this.$form.createForm(this),
      // cropper
      preview: {},
      geographicList: [],
      option: {
        img: '/avatar2.jpg',
        info: true,
        size: 1,
        outputType: 'jpeg',
        canScale: false,
        autoCrop: true,
        // 只有自动截图开启 宽度高度才生效
        autoCropWidth: 180,
        autoCropHeight: 180,
        fixedBox: true,
        // 开启宽度和高度比例
        fixed: true,
        fixedNumber: [1, 1]
      }
    }
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  mounted () {
    this.initGeographic()
    this.edit()
  },
  methods: {
    setavatar (url) {
      this.userInfo.avatar = url
      this.handleSubmit()
    },
    initGeographic () {
      getGeographic().then(res => {
        this.geographicList = res?.data || []
      })
    },
    geographicChange (value, selectedOptions) {
      this.userInfo.geographic_name = filterGeographic(selectedOptions)
    },
    edit () {
      const { form: { resetFields, setFieldsValue } } = this
      setTimeout(() => {
        resetFields()
        setFieldsValue(pick(this.userInfo, ['name', 'address', 'profile', 'phone', 'country']))
      }, 500)
    },
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          update(merge(values, { geographic: this.userInfo.geographic, geographic_name: this.userInfo.geographic_name, avatar: this.userInfo.avatar })).then(res => {
            this.confirmLoading = false
            if (res.code === 0) {
              this.visible = false
              this.$message.success(res.message)
            }
          })
        } else {
          this.confirmLoading = false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.avatar-upload-wrapper {
  height: 200px;
  width: 100%;
}

.ant-upload-preview {
  position: relative;
  margin: 0 auto;
  width: 100%;
  max-width: 180px;
  border-radius: 50%;
  box-shadow: 0 0 4px #ccc;

  .upload-icon {
    position: absolute;
    top: 0;
    right: 10px;
    font-size: 1.4rem;
    padding: 0.5rem;
    background: rgba(222, 221, 221, 0.7);
    border-radius: 50%;
    border: 1px solid rgba(0, 0, 0, 0.2);
  }
  .mask {
    opacity: 0;
    position: absolute;
    background: rgba(0, 0, 0, 0.4);
    cursor: pointer;
    transition: opacity 0.4s;

    &:hover {
      opacity: 1;
    }

    i {
      font-size: 2rem;
      position: absolute;
      top: 50%;
      left: 50%;
      margin-left: -1rem;
      margin-top: -1rem;
      color: #d6d6d6;
    }
  }

  img,
  .mask {
    width: 100%;
    max-width: 180px;
    height: 100%;
    border-radius: 50%;
    overflow: hidden;
  }
}
</style>
